import server from "../../../../utils/server.js"
var openid = wx.getStorageSync("openId");
const token = wx.getStorageSync('token');

Page({
  data: {
    option: [{
      text: '待领取',
      value: 0
    },
    {
      text: '已领取',
      value: 1
    },
    {
      text: '已过期',
      value: 2
    },
    ],
    value: 0,
    name: '',
    description: '',
    winningPrice: '',
    imageUrl: '',
    type: '',
    goodsId: '',
    image: '',
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '我的奖品'
    })
  },

  onToCheckout: function (event) {
    let _this = this
    console.log(event)
    wx.navigateTo({
      url: '/pages/checkout/checkout',


      // 向结算页面传值 (商品id)
      success(res) {
        console.log(_this.data.recordList[0].id)
        console.log("奖品状态")
        console.log(_this.data.recordList[0].type)
        res.eventChannel.emit('awardListToProduct', {
          data: {
            
            
          }
        })
      }
    })

  },

  onToProduct: function (event) {
    //console.log('123')
    let _this = this
    wx.navigateTo({
      url: '/pages/product/product',

      // 向商品详情页传值 (商品id)
      success(res) {
        console.log(_this.data.recordList[0].id)
        console.log('商品的type属性')
        console.log(_this.data.recordList[0].type)
        res.eventChannel.emit('awardListToProduct', {
          data: {
            id: _this.data.recordList[0].id,
            type: _this.data.recordList[0].type
          }
        })
      }
    })
  },


  onLoad: function (options) {
    let openid = wx.getStorageSync('kkk')
    this.getRecordList(openid);
    // Do some initialize when page load.

  },
  onShow: function () {
    // Do something when page show.
  },
  onReady: function () {
    // Do something when page ready.
  },
  onHide: function () {
    // Do something when page hide.
  },
  onUnload: function () {
    // Do something when page close.
  },
  onPullDownRefresh: function () {
    // Do something when pull down.
  },
  onReachBottom: function () {
    // Do something when page reach bottom.
  },
  onShareAppMessage: function () {
    // return custom share data when user share.
  },
  onPageScroll: function () {
    // Do something when page scroll
  },
  onResize: function () {
    // Do something when page resize
  },


  getRecordList(openId) {
    var e = this
    wx.request({
      header:{
        "Authorization": token
      },
      url: 'http://localhost:8080/record/list/' + openid,
      data: {
        // userId: openId,
      },
      success(res) {
        console.log(res.data)

        let recordListItem = res.data.data
        console.log(e.data)
        recordListItem = res.data.data.filter(el => {
          return el.type === e.data.value
        })

        console.log(recordListItem)

        recordListItem = recordListItem.map(el => {

          console.log(el.imageUrl)
          el.imageUrl = server + el.imageUrl // http://localhost:8080/image/iphone1101.jpg
          console.log(el)

          for (let index = 0; index < e.data.option.length; index++) {
            const element = e.data.option[index]
            // console.log(e.data.option)
            // console.log(element)

            if (element.value === el.type) {
              el.type = element.text
              break
            }
          }
          return el
        })

        //console.log(recordListItem)    

        e.setData({
          recordList: recordListItem,
        })
      }
    })
  },


  changeValue(e) {

    this.setData({
      value: e.detail,
    })

    this.getRecordList(wx.getStorageSync('kkk'))
  },


})